<template>
  <div>
    <!-- 主体区域 -->
    <main>
      <div class="w1200">
        <div class="gap"></div>
        <div class="wfd">
          <div class="h160">
            <span><img src="@/assets/imgs/WE_slices/WE.png" alt="" /></span>
            <span class="pdl15">
              <span class="title">{{ eSubjectType.title }}</span>
              <span class="des">{{ eSubjectType.tips }}</span>
            </span>
          </div>
          <div class="h60">
            <span class="exnum">{{ iNum }}.{{ subject.title }}</span>
            <span class="labels">
              <span v-if="subject.isJt == '1'">机经</span>
              <span v-if="subject.isJt !== '1'" style="background: #7cc3d6"
                >练习题</span
              >
              <span
                style="background: #ff5e4e"
                v-if="subject.labelStyle == 'wrong-color'"
                >错题</span
              >
              <span style="background: #13bfbf" v-if="subject.isYc == '1'"
                >本周预测</span
              >
              <span style="background: #99bbe4"
                >练习{{ practiceList.length }}次</span
              >
              <span
                style="background: #94d0f7"
                v-if="practiceList.length > 0"
                >{{ subject.titleNumber }}</span
              >
              <span style="background: #94d0f7">{{ subject.titleNumber }}</span>
            </span>
            <Label
              v-if="subject.labelStyle != undefined"
              :df="subject.labelStyle"
              :id="subject.id"
            ></Label>
          </div>
          <div class="h107">
            {{ subject.content }}
          </div>
          <div class="time">
            <span>剩余时间：{{ s_to_hs(answerTime) }}</span>
          </div>
        </div>
        <div class="gap"></div>
        <div class="myanswer">
          <div class="bt">我的答案</div>
          <div class="insert">
            <div class="bg">
              <textarea
                @input="descInput"
                v-model="answer"
                placeholder="输入你的回答"
                class="txt"
              ></textarea>
            </div>
            <span>单词数：{{ countNum }}</span>
          </div>
        </div>
        <div class="gap"></div>
        <div class="h100">
          <div class="search-box">
            <el-button @click="$router.go(0)">重做</el-button>
            <span
              ref="answer"
              @click="seeAnswer"
              :class="isAnswerShow ? 'show_answer' : ''"
              class="seeAnswer"
              >查看答案</span
            >
            <el-input placeholder="输入内容/题目" v-model="keyword">
              <template slot="append">
                <div @click="searchSubject('writingSWT')">搜索</div>
              </template>
            </el-input>
            <el-button class="prev" @click="goTo(last, '1', 'writingSWT',subject.id)"
              >上一题</el-button
            >
            <el-button class="next" @click="goTo(next, '2', 'writingSWT',subject.id)"
              >下一题</el-button
            >
            <el-button @click="addPractice()">提交</el-button>
          </div>
          <div class="answer-box" v-show="isAnswerShow">
            <div class="title">参考答案</div>
            <div class="content">{{ subject.answer }}</div>
            <div class="origin" v-if="subject.originalText">
              <div class="origin-title">原文</div>
              <div>
                {{ subject.originalText }}
              </div>
            </div>
          </div>
        </div>
        <div class="gap"></div>
        <div class="myexercise">
          <div class="bt">我的练习</div>
          <div v-show="1" class="no-data" v-if="practiceList.length == 0">
            <i class="iconfont icon-wushuju"></i>
            <div style="fontsize: 1rem">暂无数据</div>
          </div>

          <ul v-else>
            <li v-for="(item, index) in practiceList" :key="index">
              <span class="avatar">
                <img v-lazy="item.image" alt="" />
              </span>
              <span class="content">
                <span class="nickname">
                  {{ item.nickname }}
                  <span class="time">{{ item.createDate | formatTM }}</span>
                </span>

                <span class="answer-history">{{ item.answer }}</span>
              </span>
            </li>
          </ul>
        </div>
      </div>
      <!-- 做题中选题抽屉 -->
      <Drawer :img="imgUrl"></Drawer>
    </main>
  </div>
</template>

<script>
import Label from '../../label'
import Drawer from '../../drawer'
import readingUtil from "../../../../../utils/readingUtil";
import { sendPost } from "../../../../../utils/http";
import api from "../../../../../config/api";
export default {
  mixins: [readingUtil],
  components: {
    Drawer,
    Label,
  },
  data () {
    return {
      answerTime: 1200,//答题时间
      countNum: 0,
      imgUrl: ''
      , isAnswerShow: false,
      iNum: '' // 题目索引号
    }
  },
  created () {
    var query = this.$route.query;
    setInterval(this.countDownTime, 1000)
    this.getData(query.id);
    this.iNum = this.$route.query.idx;
  },
  mounted () {
    sessionStorage.setItem('activeIndex', 'loged')
  },
  methods: {
   //跳转题目页面
    goTo (id, type, name, nowId) {
      console.log("搜索中", type, id, name, nowId)
      // 没有搜索到题
      if (id == '0') {
        if (type == '1') {
          this.$msg({
            message: '没有上一题了',
            type: 'warning'
          });
        }
        if (type == '2') {
          this.$msg({
            message: '没有下一题了',
            type: 'warning'
          });
        }
        return
        // 搜到题
      }
      else {
        if (nowId > id) {
          // console.log(nowId,id);
          this.$router.push('/writingWE?id=' + id + '&idx=' + (this.iNum / 1 - 1));

        } else if (nowId < id) {
          console.log(nowId, id);
          this.$router.push('/writingWE?id=' + id + '&idx=' + (this.iNum / 1 + 1));
        }
      }
    },
    seeAnswer () {
      this.isAnswerShow = !this.isAnswerShow
      if (this.isAnswerShow) {
        this.$refs.answer.innerText = '隐藏答案'
      } else {
        this.$refs.answer.innerText = '查看答案'
      }
    },
    getData (id) {
      var that = this
      sendPost(api.getSubjectDetails, {
        id: id,
      }).then((res) => {
        console.log(res)
        that.subject = res.data
        that.eSubjectType = res.data.eSubjectType
        that.recordingCountDown = res.data.answerTime
        if (res.data.practiceList) {
          that.practiceList = res.data.practiceList
        }
        that.last = res.data.last
        that.next = res.data.next

      })
    },

    //提交作业
    addPractice () {
      if (this.answerTime <= 0) {
        this.$msg.error("超过答题时间,请刷新页面重新作答！")
        return
      }
      if (this.flag) {
        this.$msg.error("你已经作答，无需重复操作！")
        return
      }

      var that = this;
      if (this.answer == '') {
        this.$msg.error("作答后再提交！")
        return
      }
     if(this.countNum<=300){
       that.flag = true
        sendPost(api.submitAnswer, {
        id: that.$route.query.id,
        answer: that.answer,
        times: 1200 - that.answerTime
      }).then((r) => {
        that.$msg({
          message: '答案提交成功',
          type: 'success'
        });

        this.refresh(that.$route.query.id);
      })
     }else{
       this.$msg.info('请将字数控制在300以内！')
     }
    },
    // 输入字数
    descInput () {
      this.countNum = this.answer.split(' ').length
      if (this.answer.split(' ').length > 300) {
        // this.answer.slice(0,300)
        this.$msg.info("字数不能超过300")
      }
    }
  }
}
</script>

<style lang="less" scoped>
.seeAnswer {
  display: inline-block;
  background: #fffafa;
  border: 0.0625rem solid #ff5e4e;
  color: #ff5e4e;
  height: 2.5rem;
  font-size: 1.125rem;
  line-height: 2.5rem;
  border-radius: 0.3125rem;
  padding: 0 0.2875rem;
  // margin-right: 14.375rem;
  // margin-left: 7.25rem;
  cursor: pointer;
}
.show_answer {
  color: #fff;
  background-color: #ff5e4e;
}
main {
  height: 100%;
  width: 100%;
  background-color: #eff1f4 !important;
  padding-bottom: 4.625rem;
  box-sizing: border-box;
  .w1200 {
    width: 75rem;
    margin: auto;
    background-color: #eff1f4;
    .gap {
      height: 1.25rem;
    }
    .wfd {
      background-color: #fff;
      padding: 2.5rem 2.5rem 1.875rem;
      box-sizing: border-box;
      .h160 {
        height: 10rem;
        display: flex;
        padding-right: 6.875rem;
        box-sizing: border-box;
        .pdl15 {
          padding-left: 0.9375rem;
          box-sizing: border-box;
          display: flex;
          flex-direction: column;
          .title {
            font-size: 1.25rem;
            color: #333;
            font-weight: bold;
          }
          .des {
            font-size: 1rem;
            color: #999;
            line-height: 2rem;
          }
        }
      }
      .h60 {
        height: 3.75rem;
        line-height: 3.75rem;
        padding-left: 1.25rem;
        box-sizing: border-box;
        background-color: #f7f7f7;
        display: flex;
        position: relative;
        .exnum {
          width: 45rem;
          font-size: 1rem;
          color: #666;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .labels {
          display: flex;
          justify-content: flex-end;
          width: 21.25rem;
          span {
            height: 1.875rem;
            line-height: 1.875rem;
            padding: 0 0.375rem;
            box-sizing: border-box;
            background-color: #ff9b4f;
            border-radius: 0.1875rem;
            margin: auto 0.375rem;
            color: #fff;
            box-sizing: border-box;
          }
        }
      }
      .h107 {
        margin: 3.125rem 0 1.5625rem;
        width: 100%;
        background: #fffafa;
        border: 0.1875rem dashed #ff5e4e;
        border-bottom: 0.1875rem dashed #ff5e4e !important;
        box-sizing: border-box;
        padding: 1.25rem 1.75rem 1.6875rem;
        box-sizing: border-box;
        font-size: 1.125rem;
        color: #666;
        line-height: 2rem;
      }
      .time {
        font-size: 1rem;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #333333;
        span {
          margin-right: 1.875rem;
        }
      }
    }
    .myanswer {
      height: 25.5rem;
      background-color: #fff;
      padding: 0 2.5rem;
      box-sizing: border-box;
      position: relative;
      .bt {
        height: 6.25rem;
        line-height: 6.25rem;
        font-size: 2rem;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #333333;
      }
      .txt {
        height: 13.125rem;
        background-color: #f7f7f7;
        border: none;
        outline: none;
        padding: 1.875rem;
        box-sizing: border-box;
        font-size: 1.125rem;
        color: #999999;
        resize: none;
        width: 100%;
      }
      .insert {
        border-radius: 0.3125rem;
        overflow: hidden;
        .bg {
          height: 17.3125rem;
          box-sizing: border-box;
          background-color: #f7f7f7;
        }
        span {
          font-size: 1.125rem;
          color: #999;
          position: absolute;
          right: 5.3125rem;
          bottom: 3.4375rem;
        }
      }
    }
    .h100 {
      padding: 1.5625rem 2.5rem;
      background-color: #fff;
      .el-button {
        height: 2.5rem;
        color: #ff5e4e;
        font-size: 1.125rem;
        text-align: center;
        width: 5rem;
        border-radius: 0.3125rem;
        border: 0.0625rem solid #ff5e4e;
        background-color: #fffafa;
        margin-right: 1.25rem;
      }
      .el-switch {
        margin-left: 1rem;
        margin-right: 0.5rem;
      }
      .el-input {
        width: 14.875rem;
        margin-left: 13rem;
        margin-right: 1.875rem;
        ::placeholder {
          font-size: 1.125rem;
          color: #cccccc;
        }
      }
      /deep/.el-input-group__append {
        padding: 0 1rem;
        background-color: #ff5e4e;
        color: #fff;
        border: 0.0625rem solid #ff5e4e;
        box-sizing: border-box;
        font-size: 1.125rem;
        cursor: pointer;
      }
      /deep/.el-input__inner {
        height: 3.125rem;
        width: 14.875rem;
        border-radius: 0.3125rem 0 0 0.3125rem;
        overflow: hidden;
        border-color: #ededed;
        border-right: none;
      }
      .prev,
      .next {
        width: 5rem;
        height: 2.5rem;
        background: #fffafa;
        border: 0.0625rem solid #ff5e4e;
        box-sizing: border-box;
        border-radius: 0.3125rem;
        color: #ff5e4e;
        margin-top: 0.8125rem;
        margin-left: 1rem;
        padding-left: 0.75rem;
        font-size: 1.125rem;
      }
      .answer-box {
        .title {
          font-size: 1.75rem;
          color: #333;
          height: 5.3125rem;
          line-height: 5.3125rem;
        }
        .content {
          font-size: 1.125rem;
          color: #666;
          line-height: 2rem;
        }
        .origin {
          font-size: 1.125rem;
          color: #666;
          line-height: 2rem;
          .origin-title {
            font-size: 1.5rem;
            color: #ccc;
            margin-top: 1.875rem;
          }
        }
      }
    }
    .myexercise {
      background-color: #fff;
      padding: 0 2.5rem;
      box-sizing: border-box;
      padding-bottom: 3.75rem;
      .bt {
        height: 6.25rem;
        line-height: 6.25rem;
        font-size: 2rem;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #333333;
      }
      ul {
        li {
          display: flex;
          .content {
            display: flex;
            flex-direction: column;
            .nickname {
              color: #333;
              font-size: 1.5rem;
              .time {
                color: #999;
                font-size: 1rem;
                margin-left: 1rem;
              }
            }
            .answer-history {
              font-size: 1.125rem;
              color: #666;
              margin: 0.625rem;
            }
          }
        }
      }
      li {
        display: flex;
        padding-bottom: 1.5rem;
        margin-bottom: 1.2rem;
        border-bottom: 1px solid #ccc;
        .avatar {
          width: 3rem;
          height: 3rem;
          margin-right: 0.625rem;
          border-radius: 50%;
          img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
          }
        }

        .content {
          flex: 1;
          font-size: 1.5rem;
          color: #333;
          display: flex;
          flex-direction: column;

          .time {
            font-size: 1rem;
            color: #999;
            margin-left: 1.875rem;
          }

          .answer-history {
            font-size: 1.125rem;
            color: #666;
            margin-top: 1rem;
          }
        }
      }
      .content {
        flex: 1;
        margin-left: 0.9375rem;
        .uname {
          font-size: 1.5rem;
          font-family: Microsoft YaHei;
          font-weight: 400;
          color: #333333;
        }
        .reltime {
          color: #999999;
          margin-left: 1.875rem;
        }
        .txt {
          font-size: 1.125rem;
          font-family: Microsoft YaHei;
          font-weight: 400;
          color: #666666;
          line-height: 2rem;
        }
      }
    }
  }
}
.icon-lingdang1 {
  color: #eee;
  line-height: 3.75rem;
}
.avatar {
  width: 3.75rem;
  height: 3.75rem;
  border-radius: 50%;
  display: inline-block;
  vertical-align: middle;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
}
.username {
  display: inline-block;
  margin-left: 0.625rem;
  font-weight: bold;
  font-size: 1rem;
  color: #333333;
  width: 6.25rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
